package com.zhou.plugin.packagejars.util;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.JavaSdk;
import com.intellij.openapi.projectRoots.JavaSdkType;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiManager;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2016/7/8.
 */

public class Util {
	public Util() {
	}

	public static boolean matchFileNamingConventions(String fileName) {
		return fileName.matches("[^/\\\\<>*?|\"]+");
	}

	public static void iterateDirectory(Project project, HashSet<VirtualFile> directories, VirtualFile directory) {
		PsiDirectory psiDirectory = PsiManager.getInstance(project).findDirectory(directory);
		if (directory != null) {
			directories.add(psiDirectory.getVirtualFile());
			PsiDirectory[] psiDirectories = psiDirectory.getSubdirectories();
			PsiDirectory[] arr$ = psiDirectories;
			int len$ = psiDirectories.length;

			for (int i$ = 0; i$ < len$; ++i$) {
				PsiDirectory pd = arr$[i$];
				iterateDirectory(project, directories, pd.getVirtualFile());
			}
		}

	}

	public static String getTheSameStart(List<String> strings) {
		if (strings != null && strings.size() != 0) {
			int max = 888888;
			Iterator<String> sb = strings.iterator();

			while (sb.hasNext()) {
				String set = sb.next();
				if (set.length() < max) {
					max = set.length();
				}
			}

			StringBuilder var7 = new StringBuilder();
			HashSet<Character> var8 = new HashSet<>();

			for (int i = 0; i < max; ++i) {
				Iterator<String> i$ = strings.iterator();

				while (i$.hasNext()) {
					String string = i$.next();
					var8.add(Character.valueOf(string.charAt(i)));
				}

				if (var8.size() != 1) {
					break;
				}

				var7.append(var8.iterator().next());
				var8.clear();
			}

			return var7.toString();
		} else {
			return "";
		}
	}

	private static int getMinorVersion(String vs) {
		int dashIndex = vs.lastIndexOf(95);
		if (dashIndex >= 0) {
			StringBuilder builder = new StringBuilder();

			for (int ignored = dashIndex + 1; ignored < vs.length(); ++ignored) {
				char ch = vs.charAt(ignored);
				if (!Character.isDigit(ch)) {
					break;
				}

				builder.append(ch);
			}

			if (builder.length() > 0) {
				try {
					return Integer.parseInt(builder.toString());
				} catch (NumberFormatException var5) {
					;
				}
			}
		}

		return 0;
	}

	public static String getJDKPath(Project project) {
		JavaSdkVersion sdkVersion = null;
		Sdk projectJdk = null;
		int sdkMinorVersion = 0;
		HashSet<Sdk> candidates = new HashSet<>();
		Sdk defaultSdk = ProjectRootManager.getInstance(project).getProjectSdk();
		if (defaultSdk != null && defaultSdk.getSdkType() instanceof JavaSdkType) {
			candidates.add(defaultSdk);
		}

		Module[] javaSdkType = ModuleManager.getInstance(project).getModules();
		int internalJdk = javaSdkType.length;

		for (Module vs : javaSdkType) {
			Sdk candidateVersion = ModuleRootManager.getInstance(vs).getSdk();
			if (candidateVersion != null && candidateVersion.getSdkType() instanceof JavaSdkType) {
				candidates.add(candidateVersion);
			}
		}

		JavaSdk var14 = JavaSdk.getInstance();
		Iterator<Sdk> var15 = candidates.iterator();

		while (true) {
			Sdk var17;
			String var19;
			JavaSdkVersion var20;
			do {
				do {
					if (!var15.hasNext()) {
						Sdk var16 = JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk();
						if (projectJdk == null || sdkVersion == null || !sdkVersion.isAtLeast(JavaSdkVersion.JDK_1_6)) {
							projectJdk = var16;
						}

						JavaSdkType var18 = (JavaSdkType) projectJdk.getSdkType();
						String vmExecutablePath = var18.getVMExecutablePath(projectJdk);
						return vmExecutablePath.substring(0, vmExecutablePath.length() - 4);
					}

					var17 = var15.next();
					var19 = var17.getVersionString();
				} while (var19 == null);

				var20 = var14.getVersion(var19);
			} while (var20 == null);

			int candidateMinorVersion = getMinorVersion(var19);
			if (projectJdk == null) {
				sdkVersion = var20;
				sdkMinorVersion = candidateMinorVersion;
				projectJdk = var17;
			} else {
				int result = var20.compareTo(sdkVersion);
				if (result > 0 || result == 0 && candidateMinorVersion > sdkMinorVersion) {
					sdkVersion = var20;
					sdkMinorVersion = candidateMinorVersion;
					projectJdk = var17;
				}
			}
		}
	}
}
